package com.ccys.cms.controller.inventory;


import com.ccys.cms.service.inventory.PurchaseOrderDetailService;
import com.ccys.cms.service.inventory.PurchaseOrderService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.web.inventory.PurchaseOrderDto;
import com.ccys.db.dto.web.inventory.PurchaseOrderPageDto;
import com.ccys.db.dto.web.productAndService.NumEnum;
import com.ccys.db.entity.TblPurchaseOrder;
import com.ccys.db.entity.TblPurchaseOrderDetail;
import com.ccys.db.mapper.TblPurchaseOrderMapper;
import com.ccys.db.vo.web.inventory.*;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/inventory")
@Api(tags = "采购单")
public class PurchaseOrderController {


    @Resource
    private PurchaseOrderService service;
    @Resource
    private PurchaseOrderDetailService detailService;


    @Resource
    private TblPurchaseOrderMapper mapper;


    @ApiOperation(value = "新增", notes = "status : 2:received;-2:draft(草稿)")
    @PostMapping("purchaseOrder")
    @Transactional
    public ResponseResult insert(@RequestBody PurchaseOrderDto dto) {

        return service.change(dto);
    }

    @ApiOperation(value = "修改", notes = "status : 2:received;-2:draft(草稿)")
    @PutMapping("purchaseOrder")
    @Transactional
    public ResponseResult update(@RequestBody PurchaseOrderDto dto) {

        return service.change(dto);
    }


    @ApiOperation("根据id查询")
    @GetMapping("purchaseOrder/detail")
    public ResponseResult<PurchaseOrderAndDetailVo, Object> getById(String id) {

        TblPurchaseOrder entityById = service.findEntityById(id);
        List<PurchaseDetailVo> detailList = mapper.getDetailListByPurchaseOrderId(id);
        PurchaseOrderAndDetailVo vo = new PurchaseOrderAndDetailVo();
        vo.setOrder(entityById);
        vo.setDetailList(detailList);
        return ResponseResult.success(vo);
    }


    @ApiOperation("根据id删除")
    @DeleteMapping("purchaseOrder")
    @Transactional
    public ResponseResult delete(String id) {
        TblPurchaseOrder model = new TblPurchaseOrder();
        model.setDelFlag(NumEnum.DEL.getCode());
        model.setId(id);
        int i = service.updateEntityById(model);
        service.deleteDetail(id);
        return ResponseResult.update(i);
    }


    @ApiOperation(value = "查询分页", notes = "排序字段 t1.code(指的是purchaseNo列) / t3.group_name / t4.name(指的是supplier列) / t1.total_amount /t1.delivery_date  /t2. user_fullname / t1.created_on")
    @GetMapping("purchaseOrder")
    public ResponseResult<PageInfo<PurchaseOrderVo>, Object> getPage(PurchaseOrderPageDto dto) {
        return service.getPage(dto);
    }

    @ApiOperation("查询采购单集合")
    @GetMapping("purchaseOrder/list")
    public ResponseResult<List<GetPurchaseOrderListVo>,Object> getPurchaseOrderList(){
        List<GetPurchaseOrderListVo> orderList = mapper.getOrderList();
        return ResponseResult.success(orderList);
    }



    @ApiOperation("查询采购单detail列表集合")
    @GetMapping("purchaseOrder/detail/list")
    public ResponseResult<List<GetPurchaseDetailListVo>, Object> getDetailList(@RequestParam(required = false) String purchaseOrderId,@RequestParam(name = "productId",required = false) String productId) {
        List<GetPurchaseDetailListVo> detailList = mapper.getDetailList(purchaseOrderId,productId);
        return ResponseResult.success(detailList);
    }


}
